package com.gxa.modules.pc.fee.controller;

import com.gxa.common.utils.FilePortUtil;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.model.dto.RefundHistoryDto;
import com.gxa.modules.model.dto.RefundSearchDto;
import com.gxa.modules.pc.fee.service.RefundHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 退款记录表 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:31:54
 */
@Api(tags = "退款记录")
@RestController
@RequestMapping("/refund-history")
public class RefundHistoryController {
    @Autowired
    private RefundHistoryService refundHistoryService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }



    @PostMapping("/query")
    @ApiOperation(value = "分页带条件退款记录查询")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success", response = RefundHistoryDto.class)
    })
    public Result<PageUtils> query(@RequestBody RefundSearchDto refundSearchDto){
      try {
          PageUtils pageUtils = refundHistoryService.queryByRefundSearchDto(refundSearchDto);

          return new Result<PageUtils>().ok(pageUtils);
      }catch (Exception e){
          e.printStackTrace();
          return new Result().error("查询失败");
      }
    }


    @GetMapping("/query/{id}")
    @ApiOperation(value = "退款详细信息查询")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success", response = RefundHistoryDto.class)
    })
    public Result queryById(@PathVariable("id") Integer id){
        try {
            RefundHistoryDto refundHistoryDto = this.refundHistoryService.queryById(id);
            return new Result<>().ok(refundHistoryDto);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败");
        }
    }

    @GetMapping(value = "/fileport")
    @ApiOperation("导出退款记录表")
    public void filePort(HttpServletResponse response) {
            //导出的表名
            String title = "导出退款记录";
            //表中第一行表头字段
            String[] headers = {"主键id", "操作时间", "退款编号", "患者姓名", "就诊卡号", "来源", "业务类型", "退款方式", "退款金额（元）", "退款状态", "卡内余额"};
            //实际数据结果集
            List<RefundHistoryDto> refundHistoryDtoList = this.refundHistoryService.queryList();
            //具体需要写入excel需要哪些字段，这些字段取自RechargeDetailRecordDto类，也就是上面的实际数据结果集的泛型
            List<String> listColumn = Arrays.asList("id", "operationTime", "orderId", "name", "visitNum", "source", "businessType", "refundRoute", "refundAmount", "refundStatus", "cardBalance");
            try {
                FilePortUtil.exportExcel(response, title, headers, refundHistoryDtoList, listColumn);
            } catch (Exception e) {
                e.printStackTrace();
            }

    }
}
